<template>
  <div class="channel science-channel">
    <!-- 医生发布入口 -->
    <div class="create-post" v-if="userRole === 'doctor'">
      <el-button type="primary" @click="showPostDialog">发布科普文章</el-button>
    </div>
    <el-alert v-else type="info" :closable="false">
      只有认证宠物医生可以发布科普文章
    </el-alert>

    <!-- 文章列表 -->
    <div class="post-list">
      <el-card v-for="post in posts" :key="post.id" class="post-item">
        <div slot="header">
          <h3>{{ post.title }}</h3>
          <div class="post-meta">
            <el-tag type="success">{{ post.petType }}</el-tag>
            <span class="author">作者：{{ post.author }}</span>
            <span class="time">{{ post.time }}</span>
          </div>
        </div>
        <div class="post-content">{{ post.content }}</div>
        <comment-section :comments="post.comments" @submit="handleComment"/>
      </el-card>
    </div>

    <!-- 发布对话框 -->
    <el-dialog title="发布科普文章" :visible.sync="postDialogVisible">
      <el-form :model="postForm" :rules="rules">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="postForm.title" />
        </el-form-item>
        <el-form-item label="宠物类型" prop="petType">
          <el-select v-model="postForm.petType">
            <el-option v-for="t in petTypes" :key="t" :label="t" :value="t"/>
          </el-select>
        </el-form-item>
        <el-form-item label="文章内容" prop="content">
          <el-input type="textarea" :rows="6" v-model="postForm.content"/>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="postDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitPost">发布</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ['userRole'],
  data() {
    return {
      postDialogVisible: false,
      petTypes: ['犬类', '猫类', '其他宠物'],
      postForm: {
        title: '',
        petType: '',
        content: ''
      },
      rules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        petType: [{ required: true, message: '请选择宠物类型', trigger: 'change' }],
        content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      },
      posts: [
        {
          id: 1,
          title: '犬类疫苗接种指南',
          petType: '犬类',
          content: '...详细内容...',
          author: '张医生',
          time: '2023-08-15',
          comments: []
        }
      ]
    }
  },
  methods: {
    showPostDialog() {
      this.postDialogVisible = true
    },
    submitPost() {
      // 提交逻辑
    },
    // eslint-disable-next-line no-unused-vars
    handleComment(content) {
      // 处理评论
    }
  }
}
</script>